package com.atguigu.gulimall.search.thread;


import org.apache.tomcat.util.threads.ThreadPoolExecutor;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 2022/3/17 21:29 星期四
 * ThreadTest
 * Created by ASIA 乔帅阳 .
 * --
 * --
 */
public class ThreadTest {
    public static final ExecutorService executor = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main................start.....");



       /* CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果: " + i);
        }, executor);*/

 /*
      //方法完成后的感知；
       CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 0;
            System.out.println("运行结果: " + i);
            return i;
        }, executor).whenComplete((res,excption)->{
            //虽然能得到异常信息，但是没法修改返回数据
             System.out.println("异步任务成功完成了。。。结果是："+res+";异常是；"+excption);
        }).exceptionally(throwable -> {
            //可以感知异常，同时返回默认值
            return 10;
        });
        Integer integer = future.get();
        System.out.println("main................stop....."+integer);*/



        //方法完成后的处理
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 4;
            System.out.println("运行结果: " + i);
            return i;
        }, executor).handle((res,thr)->{
            if(res!=null){
                return res * 2;
            }
            if(thr!=null){
                return 1;
            }
            return 0;
        });
        Integer integer = future.get();
        System.out.println("main................stop....."+integer);
    }



   // ThreadPoolExecutor executor =  new ThreadPoolExecutor();
}
